package com.smartleanx.module.hrm.service.memorandum;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.C;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.smartleanx.framework.common.util.collection.CollectionUtils;
import com.smartleanx.framework.security.core.util.SecurityFrameworkUtils;
import com.smartleanx.module.hrm.context.EmployeeContextHolder;
import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeDO;
import com.smartleanx.module.hrm.intergration.AdminUserServiceProvider;
import com.smartleanx.module.system.api.user.dto.AdminUserRespDTO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.util.SystemPropertyUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

import com.smartleanx.module.hrm.controller.admin.memorandum.vo.*;
import com.smartleanx.module.hrm.dal.dataobject.memorandum.MemorandumDO;
import com.smartleanx.framework.common.pojo.PageResult;
import com.smartleanx.framework.common.pojo.PageParam;
import com.smartleanx.framework.common.util.object.BeanUtils;

import com.smartleanx.module.hrm.dal.mysql.memorandum.MemorandumMapper;

import static com.smartleanx.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.smartleanx.module.hrm.enums.ErrorCodeConstants.*;

/**
 * 备忘录 Service 实现类
 *
 * @author 精益大师
 */
@Service
@Validated
public class MemorandumServiceImpl implements MemorandumService {

    @Resource
    private MemorandumMapper memorandumMapper;

    @Override
    public Long createMemorandum(MemorandumSaveReqVO createReqVO) {
        MemorandumDO memorandumDO = BeanUtils.toBean(createReqVO, MemorandumDO.class);
        memorandumMapper.insert(memorandumDO);
        return memorandumDO.getId();
    }

    @Override
    public void deleteMemorandum(Long id) {
        // 校验存在
        validateMemorandumExists(id);
        // 删除
        memorandumMapper.deleteById(id);
    }

    private void validateMemorandumExists(Long id) {
        if (memorandumMapper.selectById(id) == null) {
            throw exception(MEMORANDUM_NOT_EXISTS);
        }
    }

    public List<MemorandumListRespVO> getMemorandumList(String date) {
        LocalDate localDate = LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        // 获取当前日期相邻前后两个月的备忘录明细
        List<MemorandumDO> doList = memorandumMapper.selectList(new LambdaQueryWrapper<MemorandumDO>()
                .eq(MemorandumDO::getCreator, SecurityFrameworkUtils.getLoginUserId())
                .between(MemorandumDO::getMemosDate, localDate.plusMonths(-1), localDate.plusMonths(1)));
        if (CollUtil.isNotEmpty(doList)) {
            List<MemorandumRespVO> list = CollectionUtils.convertList(doList, m -> {
                MemorandumRespVO vo = new MemorandumRespVO();
                vo.setId(m.getId());
                vo.setMemosDate(m.getMemosDate());
                vo.setMemos(m.getMemos());
                return vo;
            });
            Map<LocalDate, List<MemorandumRespVO>> collect = list.stream().collect(Collectors.groupingBy(MemorandumRespVO::getMemosDate));
            List<MemorandumListRespVO> respVOList = new ArrayList<>();
            for (LocalDate key : collect.keySet()) {
                MemorandumListRespVO vo = new MemorandumListRespVO();
                vo.setMemosDate(key);
                vo.setMemorandumRespVOList(collect.get(key));
                respVOList.add(vo);
            }
            return respVOList;

        }
        return Collections.emptyList();
    }


}
